package com.cjh.weixin.controller;

import com.cjh.common.entity.Address;
import com.cjh.common.response.Result;
import com.cjh.common.service.AddressService;
import com.cjh.weixin.common.handler.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cjh
 * @since 2022-01-25
 */
@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    AddressService addressService;

    @GetMapping("/getAllAddress")
    public Result getAllAddress(){
        return addressService.getAllAddress(UserThreadLocal.get().getId());
    }

    @PostMapping("/addAddress")
    public Result addAddress(@RequestBody Address address){
        address.setUserId(UserThreadLocal.get().getId());
        return addressService.addAddress(address);
    }
    @GetMapping ("/getDeafultAddress")
    public Result getDeafultAddress(){
        return addressService.getDeafultAddress(UserThreadLocal.get().getId());
    }
    @GetMapping("/updateDefaultAddressById")
    public Result updateDefaultAddressById(@RequestParam Integer id,@RequestParam Integer defaultAddress){
        return addressService.updateDefaultAddressById(id,defaultAddress,UserThreadLocal.get().getId());
    }

    @PostMapping("/updateAddress")
    public Result updateAddress(@RequestBody Address address){
        return addressService.updateAddress(address);
    }

    @GetMapping("/deleteAddress")
    public Result deleteAddress(@RequestParam Integer id){
        return addressService.deleteAddress(id);
    }
}
